﻿using pTools.cMethods;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools.ChildWin
{
    public partial class selectUser : Form
    {
        cMethods.DBHelper db;
        Dictionary<string, string> dic;
        public selectUser(Dictionary<string,string> pars)
        {
            InitializeComponent();
            db = new cMethods.DBHelper();
            dic = pars;
        }

        private void selectUser_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = "用户选择";
                loadData();
                createTable();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void loadData()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select userCode,userName,gxCode,gxName,gangweiName,gangwei ");
            sb.Append("from V_Users_GangWei where isdel=0 ");
            //sb.Append(" and gxCode='"+dic["gxCode"]+"' and machine='"+ dic["machine"] +"'");
            if (!string.IsNullOrWhiteSpace(tbCode.Text))
            {
                sb.Append(" and userCode like '%" + tbCode.Text.Trim() + "%' ");
            }
            if (!string.IsNullOrWhiteSpace(tbName.Text))
            {
                sb.Append(" and userName like '%" + tbName.Text.Trim() + "%' ");
            }
            sb.Append(" order by id asc");
            var table = db.getData(sb.ToString(), "");
            this.dvList.DataSource = table;
            this.dvList.AutoGenerateColumns = false;
        }
        private DataTable cTable = new DataTable();
        private void createTable()
        {
            if (cTable.Columns.Count < 1)
            {
                cTable.Columns.Add("userCode");
                cTable.Columns.Add("userName");
                cTable.Columns.Add("gangwei");
            }
        }
        List<string> rList = new List<string>();
        private void dvList_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (dvList.RowCount > 0)
                {
                    string ucode = this.dvList.CurrentRow.Cells["userCode"].Value.ToString();
                    if (!rList.Contains(ucode))
                    {
                       
                        DataRow r = cTable.NewRow();
                        r["userCode"] = ucode;
                        r["userName"] = this.dvList.CurrentRow.Cells["userName"].Value.ToString();
                        r["gangwei"] = this.dvList.CurrentRow.Cells["gangwei"].Value.ToString();
                        cTable.Rows.Add(r);
                        this.dvCList.DataSource = cTable;
                        this.dvCList.AutoGenerateColumns = false;
                        rList.Add(ucode);
                    }
                    
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dvCList_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                string ucode = this.dvCList.CurrentRow.Cells["uCode"].Value.ToString();
                for(int i=0;i<rList.Count;i++)
                {
                    if (rList[i] == ucode)
                    {
                        rList.Remove(rList[i]); break;
                    }
                }
                for (int i = 0; i <cTable.Rows.Count; i++)
                {
                    if (cTable.Rows[i]["userCode"].ToString() == ucode)
                    {
                        cTable.Rows[i].Delete();
                    }
                }
                    this.dvCList.DataSource = cTable;
                    this.dvCList.AutoGenerateColumns = false;
          
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnConfirm_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CommonMethods.selectUserCodes = "";
            CommonMethods.selectUserNames = "";
            for (int i = 0; i < cTable.Rows.Count; i++)
            {
                CommonMethods.selectUserNames = CommonMethods.selectUserNames + cTable.Rows[i]["userName"].ToString() + ",";
                CommonMethods.selectUserCodes = CommonMethods.selectUserCodes + cTable.Rows[i]["userCode"].ToString() + ",";
            }
            CommonMethods.selectUserNames = CommonMethods.selectUserNames.TrimEnd(',');
            CommonMethods.selectUserCodes = CommonMethods.selectUserCodes.TrimEnd(',');
            this.Close();
        }

        private void btnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {

                loadData();
     
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
